package com.icegame.server;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 应用模块名称<p>
 * 代码描述<p>
 * Company: 南京雪糕网络科技有限公司<p>
 *
 * @author 18486
 * @since 2019/9/28 14:22
 */
@SuppressWarnings("ALL")
public class FTPServerWorker implements Runnable{

    private Socket clientSocket;
    private int num;
    private boolean open = true;
    private String fileName;
    public FTPServerWorker(Socket clientSocket, int num){
        this.clientSocket = clientSocket;
        this.num = num;
    }

    private static final Logger log = LoggerFactory.getLogger(FTPServerWorker.class);
    private static final String DEFAULT_PATH = "C:/Users/18486/Desktop/ftpserver";

    @Override
    public void run() {

        log.info("worket" + num+ "已经开始服务这台客户端");

        try {
            DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            outToServer.writeBytes("hello" + clientSocket.getInetAddress()+ "\n");
            outToServer.flush();

            while(open){
                String clientResponse=inFromClient.readLine();
                log.info("客户端请求：" + clientResponse);
                switch(clientResponse){
                    case "USER":
                        outToServer.write("指定密码\n".getBytes());
                        outToServer.flush();
                        break;
                    case "PASS":
                        outToServer.write("登录成功\n".getBytes());
                        outToServer.flush();
                        break;
                    case "QUIT":
                        outToServer.write("退出\n".getBytes());
                        outToServer.flush();
                        open=false;
                        break;
                    case "LIST":
                        File dir=new File(DEFAULT_PATH);
                        File[] listOfFiles=dir.listFiles();
                        for(File file:listOfFiles){
                            if(file.isFile()){
                                System.out.println(file.getName());
                                String fname=file.getName();
                                outToServer.writeBytes(fname+"\n");
                            }
                        }
                        outToServer.writeBytes("end\n");
                        outToServer.flush();
                        break;
                    case "PASV":
                        int randomPort= ThreadLocalRandom.current().nextInt(5500,6000);
                        String randomPortString=Integer.toString(randomPort);
                        System.out.println("1");
                        outToServer.writeBytes(randomPortString+"\n");
                        System.out.println("2");
                        FTPServerSender threadSender=new FTPServerSender(randomPort,fileName);
                        new Thread(threadSender).start();
                        break;
                    case "RETR":
                        fileName=inFromClient.readLine();
                        System.out.println(fileName);
                        break;
                    default:
                        outToServer.write("错误 !!!!\n".getBytes());
                        outToServer.flush();
                        open=false;
                }
            }

            clientSocket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                clientSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
